import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        num:100
    },
    mutations: {
        addNum(state, val=1) {
            state.num+=val
        },
        subNum(state, val=1) {
            state.num-=val
        },
        setNum(state, val) {
            state.num=val
        }
    },
    actions: {
        asyncaddnum(store, val) {
            setTimeout(() => {
                store.commit('addNum',val)
            },1000)
        },
        asyncsubnum(store, val) {
            setTimeout(() => {
                store.commit('subNum',val)
            },2000)
        }
    }
})
export default store